<template>
	<view>
		<view class="container">
			<view class="list-cell b-b m-t" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">
					修改头像
				</text>
				<view class="example-body">
					<view v-if="imgUrl">
						<image :src="imgUrl" style="width: 100rpx;height: 100rpx;border-radius: 50%;border: 2rpx solid #000" @tap="openChooseImg">
						</image>
					</view>
					<view v-else>
						<text style="position: relative;left: 78rpx;top: -40rpx;"@tap="openChooseImg">选择</text>
						<image style="width: 100rpx;height: 100rpx;border-radius: 50%;border: 2rpx solid #000;" @tap="openChooseImg">
						</image>
					</view>
				</view>
			</view>
			<view class="list-cell b-b " hover-class="cell-hover" :hover-stay-time="50"
				@click="inputShowModal">
				<text class="cell-tit">
					修改昵称
				</text>
				<view class="example-body">
					{{user.nickname}}
				</view>
				<text class="cell-more yticon icon-you" />
			</view>
			<view class="list-cell b-b" hover-class="cell-hover" :hover-stay-time="50" @click="this.show = true">
				<view class="cell-tit">
					修改性别
				</view>
				<u-picker :show="show" :columns="columns" keyName="name" @cancel="closePicker" @confirm="getGender"></u-picker>
				<view class="example-body">
					{{user.genderName}}
				</view>
				<text class="cell-more yticon icon-you" />
			</view>
			<view class="list-cell b-b" hover-class="cell-hover" :hover-stay-time="50" @click="genderShowPhoneModal">
				<text class="cell-tit">
					修改手机号
				</text>
				<view class="example-body">
					{{user.phone}}
				</view>
				<text class="cell-more yticon icon-you" />
			</view>
			<neil-modal :show="inputShow" title="修改昵称" @close="cancel" @cancel="cancel" @confirm="confirm">
				<input v-model="inputContent" style="margin:20upx" placeholder="请输入内容">
			</neil-modal>

			<neil-modal :show="inputShowPhone" title="修改手机号" @close="cancel" @cancel="cancel" @confirm="confirmPhone">
				<input v-model="phone" style="margin:20upx" placeholder="请输入...">
			</neil-modal>
		</view>
		<view class="infoButton">
			<u-button type="success" text="点击修改" @click="goToProfile"></u-button>
		</view>
	</view>
</template>

<script>
	import neilModal from '@/components/neil-modal/neil-modal.vue'

	import {
		mapState
	} from 'vuex'
	export default {
		components: {
			neilModal
		},

		data() {
			return {
				inputShow: false,
				feild: undefined,
				inputContent: '',
				inputShowPhone: false,
				gender: undefined,
				phone: undefined,
				show: false,
				columns: [
					[{
						name: '未知',
						value: 0
					}, {
						name: '女',
						value: 1
					}, {
						name: '男',
						value: 2
					}]
				],
				uploadUrl: ' ',
				imageStyles: {
					width: 65,
					height: 65,
					border: {
						radius: '50%'
					}
				},
				user: {},
				imgUrl: ''
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				this.$api.request('user', 'info').then(res => {
					this.imgUrl = res.data.avatarUrl
					this.user = res.data
					if (this.user.gender == 0) {
						this.user.genderName = '未知'
					} else if (this.user.gender == 1) {
						this.user.genderName = '女'
					} else if (this.user.gender == 2) {
						this.user.genderName = '男'
					}
				})
			},
			getGender(e){
				this.show = false
				this.user.genderName = e.value[0].name
				this.user.gender = e.value[0].value
			},
			closePicker(){
				this.show = false
			},
			openChooseImg() {
				uni.chooseImage({
					count: 1, //最多可选择的张数
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择，和摄像头功能，默认二者都有
					success: res => {
						this.imgUrl = res.tempFilePaths[0] //把选择的头像赋值给imgurl
						const that = this
						const obj = {
							avatarUrl: that.imgUrl
						}
						that.$api.request('user', 'syncUserInfo', obj).then(res => {
							that.userInfo.avatarUrl = that.imgUrl
						})
						console.log(that.imgUrl, '头像res')

					}
				});
			},
			// 点击修改方法
			goToProfile() {
				const that = this
				console.log(that.user.gender,46484);
				that.$api.request('user', 'syncUserInfo',{
					nickname:that.user.nickname,
					gender:that.user.gender,
					phone:that.user.phone,
					avatarUrl:that.imgUrl
				}).then(res => {
					uni.showToast({
						title: '修改成功',
						duration: 3000 //单位毫秒
					});
					setTimeout(function() {
						uni.navigateBack()
					}, 800);
				})	
			},
			cancel() {
				this.inputShow = false
				this.inputShowPhone = false
			},
			inputShowModal() {
				this.inputShow = true
				this.inputContent = this.user.nickname
			},
			genderShowPhoneModal() {
				this.inputShowPhone = true
				this.phone = this.user.phone
			},
			confirmPhone() {
				const that = this
				if (!that.phone) {
					that.inputShowPhone = true
					that.$api.msg('输入不能为空')
					return
				} else if (!(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.inputShowPhone = true
					that.$api.msg('手机号码输入有误')
					return
				}
				that.user.phone = that.phone
			},
			confirm() {
				const that = this
				if (!that.inputContent) {
					that.$api.msg('输入不能为空')
					return
				}
				that.user.nickname = that.inputContent
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
	}

	page {
		background: $page-color-base;
	}

	.list-cell {
		display: flex;
		align-items: baseline;
		padding: 20upx $page-row-spacing;
		line-height: 60upx;
		position: relative;
		background: #fff;
		justify-content: center;

		&.log-out-btn {
			margin-top: 40upx;

			.cell-tit {
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		&.m-t {
			margin-top: 16upx;
			display: flex;
			align-items: center;
		}

		.cell-more {
			align-self: baseline;
			font-size: $font-lg;
			color: $font-color-light;
			margin-left: 10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: $font-base;
			color: $font-color-light;
		}

		switch {
			transform: translateX(16upx) scale(.84);
		}
	}

	.infoButton {
		display: flex;
		margin: 0 auto;
		padding: 30rpx;
	}
</style>
